package com.distributeredis.redis_springboot.spring_fast_json.d01_fastjsontest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.junit.Test;

import java.text.SimpleDateFormat;
import java.util.*;

public class TestFastJson {
    public String getDate() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }

    // json转java，list，map对象
    @Test
    public void test1() {
        UserPeople userPeople1 = new UserPeople("张三",22,"河南",getDate());
        String people1 = JSON.toJSONString(userPeople1);

        UserPeople userPeople2 = new UserPeople("李四",20,"广东",getDate());
        List<UserPeople> peopleList = Arrays.asList(userPeople1, userPeople2);
        String peopleLists = JSON.toJSONString(peopleList);

        HashMap<String,Object> map = new HashMap<>();
        map.put("people1",userPeople1);
        map.put("people2",userPeople2);
        String mapString = JSON.toJSONString(map);

        // json转java   parseObject
        UserPeople userPeople = JSON.parseObject(people1, UserPeople.class);
        System.out.println(userPeople);

        // json 转list
        List<UserPeople> userPeopleList = JSON.parseArray(peopleLists, UserPeople.class);
        System.out.println(userPeopleList);

        // json 转map  方式一
        Map parse = (Map) JSON.parse(mapString);
        System.out.println(parse);
        // 方式二
        Map<String, UserPeople> peopleMap = JSON.parseObject(mapString, new TypeReference<Map<String, UserPeople>>() {});
        System.out.println(peopleMap);

    }
}
